package com.mit.web.business;

import java.util.Date;
import java.util.List;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.exception.BaseException;
import com.mit.common.manufacture.dto.FactoryDTO;
import com.mit.common.manufacture.dto.MODTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.ManufactureTokens;
import com.mit.common.token.Tokens;

public class ManufactureService extends RemoteService {
	private final String FACADE_NAME = "ManufactureFacade";
	@Override
	public String getFacadeName() {

		return FACADE_NAME;
	}

	public FactoryDTO saveFactory(FactoryDTO to,UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveFactory", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_FACTORY_DTO, to);
		ResultContext rlt  = ServiceLocator.getInstance().doBusiness(rqs);	
		FactoryDTO rtn = (FactoryDTO) rlt.getResult(ManufactureTokens.KEY_FACTORY_DTO);
		return rtn;

	}
	
	public List<FactoryDTO> retrievePagedFactorys(String searchKey, Integer pageNo, Integer pageSize, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedFactorys", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_FACTORY_NAME, searchKey);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<FactoryDTO> rtn = (List<FactoryDTO>) rlt.getResult(ManufactureTokens.KEY_FACTORY_LIST);
		return rtn;
	}
	
	public Integer retrieveFactoryCount(String searchKey, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveFactoryCount", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_FACTORY_NAME, searchKey);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		Integer rtn = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rtn;
	}
	
	public List<FactoryDTO> retrieveFactoryBasicInfo(String searchName,  UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveFactoryBasicInfo", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_FACTORY_NAME, searchName);
		
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<FactoryDTO> rtn = (List<FactoryDTO>) rlt.getResult(ManufactureTokens.KEY_FACTORY_LIST);
		return rtn;
	}
	
	public List<MODTO> retrievePagedMO(String code, Integer factoryId, Date dateFrom, 
			Date dateTo, String state, Integer pageNo, Integer pageSize, UserDTO currentUser)throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedMo", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_MO_CODE, code);
		if(factoryId > 0)
			rqs.addParameter(ManufactureTokens.KEY_FACTORY_ID, factoryId);
		rqs.addParameter(Tokens.KEY_DATE_FROM, dateFrom);
		rqs.addParameter(Tokens.KEY_DATE_TO, dateTo);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<MODTO> rtn = (List<MODTO>) rlt.getResult(ManufactureTokens.KEY_MO_LIST);
		return rtn;
	}
	
	public Integer retrieveMOCount(String code, Integer factoryId, Date dateFrom, 
			Date dateTo, String state, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveMoCount", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_MO_CODE, code);
		if(factoryId > 0)
			rqs.addParameter(ManufactureTokens.KEY_FACTORY_ID, factoryId);
		rqs.addParameter(Tokens.KEY_DATE_FROM, dateFrom);
		rqs.addParameter(Tokens.KEY_DATE_TO, dateTo);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		Integer rtn = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rtn;
	}
	
	public MODTO retrieveMO(Integer id, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveMO", currentUser);
		rqs.addParameter(Tokens.KEY_ID, id);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		MODTO rtn = (MODTO) rlt.getResult(ManufactureTokens.KEY_MO_DTO);
		return rtn;
	}
	public MODTO saveMO(MODTO mo,  UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveMo", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_MO_DTO, mo);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		MODTO rtn = (MODTO) rlt.getResult(ManufactureTokens.KEY_MO_DTO);
		return rtn;
	}

	public void deleteMO(Integer id, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("deleteMO", currentUser);
		rqs.addParameter(Tokens.KEY_ID, id);
		ServiceLocator.getInstance().doBusiness(rqs);	
	}
	
	public MODTO updateMOState(MODTO mo, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("updateMoState", currentUser);
		rqs.addParameter(ManufactureTokens.KEY_MO_DTO, mo);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		MODTO rtn = (MODTO) rlt.getResult(ManufactureTokens.KEY_MO_DTO);
		return rtn;	
	}
}
